package com.atguigu.bigdata.spark.core.rdd.operator.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

import java.util.Arrays;
import java.util.List;

public class Spark13_RDD_Operator_Transform_JAVA {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("sparkCore");
        JavaSparkContext sc = new JavaSparkContext(conf);

        // TODO 算子 - 双Value类型

        // 交集，并集和差集要求两个数据源数据类型保持一致
        // 拉链操作两个数据源的类型可以不一致

        List<Integer> list1 = Arrays.asList(1,2,3,4,5);
        List<Integer> list2 = Arrays.asList(2,3,4,5,6);
        List<String> list3 = Arrays.asList("1","2","3","4","5");

        JavaRDD<Integer> rdd1 = sc.parallelize(list1, 2);
        JavaRDD<Integer> rdd2 = sc.parallelize(list2,2);
        JavaRDD<String> rdd3 = sc.parallelize(list3, 3);

        //intersection
        JavaRDD<Integer>  intersection = rdd1.intersection(rdd2);
        System.out.println(intersection.collect().toString());
        //union
        JavaRDD<Integer> union = rdd1.union(rdd2);
        System.out.println(union.collect().toString());
        //subtract
        JavaRDD<Integer> subtract = rdd1.subtract(rdd2);
        System.out.println(subtract.collect().toString());
        //zip 可以合并不同类型rdd 相同数据拉一起
        JavaPairRDD<Integer,Integer> zip = rdd1.zip(rdd2);
        System.out.println(zip.collect().toString());

        JavaPairRDD<Integer,String> zip1 = rdd1.zip(rdd3);
        System.out.println(zip1.collect().toString());

        sc.stop();
    }
}
